package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.ILineTaskService;
import com.cn.kehong.api.IProductPeiTouService;
import com.cn.kehong.api.IXunjianTaskService;
import com.cn.kehong.domain.LineTask;
import com.cn.kehong.domain.XunjianTask;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/xunjianTask")
public class XunjianTaskController {

    @Autowired
    private IXunjianTaskService iXunjianTaskService;
    @Autowired
    private IProductPeiTouService productPeiTouService;
    //获取巡检任务的列表数据
    @RequestMapping("/getXunjianTaskList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<XunjianTask>> getXunjianTaskList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<XunjianTask>> response = new JsonResponse<>();
        List<XunjianTask> xunjianTasks = iXunjianTaskService.getXunjianTaskList(queryForm);
        response.setResult(xunjianTasks);
        response.setSuccess(true);
        return response;
    }


    //巡检下载接口
    @ResponseBody
    @RequestMapping("/exportXunjianTask")
    public void exportLineTask(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setType(jsonObject.getString("type"));
        }
        iXunjianTaskService.exportXunjianTask(queryForm,response);
    }


    //生产线边任务数量(包含带提交跟已提交的数据)
    @RequestMapping("/getXunjianTaskListByApp")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getXunjianTaskListByApp(@RequestBody QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  xunjianTaskListByApp = iXunjianTaskService.getXunjianTaskListByApp(queryForm);
        response.setResult(xunjianTaskListByApp);
        response.setSuccess(true);
        return response;
    }


    //生产线边任务数量
    @RequestMapping("/getXunjianTaskCountByApp")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getXunjianTaskCountByApp(@RequestBody QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  xunjianCount = iXunjianTaskService.getXunjianTaskCountByApp(queryForm);
        response.setResult(xunjianCount);
        response.setSuccess(true);
        return response;
    }


    // 根据厂房车间任务名称查询
    @RequestMapping("/getNameByXunjianList")
    @ResponseBody
    public JsonResponse<List<XunjianTask>> getNameByXunjianList(@RequestBody QueryForm queryForm) throws Exception {
        JsonResponse<List<XunjianTask>> response = new JsonResponse<>();
        List<XunjianTask> xunjianTasks = iXunjianTaskService.getNameByXunjianList(queryForm);
        response.setResult(xunjianTasks);
        response.setSuccess(true);
        return response;
    }


    //巡检新增接口
    @RequestMapping("/addXunjianTask")
    @ResponseBody
    public JsonResponse<String> addXunjianTask(@RequestBody XunjianTask xunjianTask) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iXunjianTaskService.addXunjianTask(xunjianTask);
        return CommonUtil.getResponseData(response,result);
    }

    // 批次/本班今日或其他修改状态（修改）
    @RequestMapping("/updateXunjianTask")
    @ResponseBody
    public JsonResponse<String> updateLineTask(@RequestBody XunjianTask xunjianTask) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = iXunjianTaskService.updateLineTask(xunjianTask);
        return CommonUtil.getResponseData(response, result);
    }

//    //巡检任务数量
//    @RequestMapping("/getXunjianTaskCount/{frontToken}")
//    @ResponseBody
//    public JsonResponse<Map<String,Object>> getXunjianTaskCount( @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
//        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
//        Map<String,Object>  xunjianTaskCount = iXunjianTaskService.getXunjianTaskCount();
//        response.setResult(xunjianTaskCount);
//        response.setSuccess(true);
//        return response;
//    }


    //主页
    @RequestMapping("/getXunjianTaskToday/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getXunjianTaskToday(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Integer>> response = new JsonResponse<Map<String,Integer>>();
        Map<String,Integer>  xunjianCount = iXunjianTaskService.getXunjianTaskToday();
        response.setResult(xunjianCount);
        response.setSuccess(true);
        return response;
    }


    //取消接口
    @RequestMapping("/xunjianTaskCancel")
    @ResponseBody
    public JsonResponse<String> xunjianTaskCancel(@RequestBody XunjianTask xunjianTask) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iXunjianTaskService.xunjianTaskCancel(xunjianTask.getId());
        return CommonUtil.getResponseData(response,result);
    }


    //巡检任务数量上方的数量(新)
    @RequestMapping("/getXunjianTaskByNewCount/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getXunjianTaskByNewCount(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  xunjianTaskByNewCount = iXunjianTaskService.getXunjianTaskByNewCount(queryForm);
        response.setResult(xunjianTaskByNewCount);
        response.setSuccess(true);
        return response;
    }
    //通过ma mb mc md查下方/巡检任务数量(新)
    @RequestMapping("/getXunjianTaskByNewType/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getXunjianTaskByNewType(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  xunjianTaskByNewType = iXunjianTaskService.getXunjianTaskByNewType(queryForm);
        response.setResult(xunjianTaskByNewType);
        response.setSuccess(true);
        return response;
    }

}
